package com.chenbei.erp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.domain.vo.TreeNode;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.common.util.TreeNodeUtil;
import com.chenbei.erp.domain.ProductCategory;
import com.chenbei.erp.vo.req.ProductCategoryReq;
import com.chenbei.erp.mapper.ProductCategoryMapper;
import com.chenbei.erp.service.IProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Override
    public void add(ProductCategory productCategory) {
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(productCategory.getPid() == null) {
            productCategory.setPid(0L);
        }

        lambdaQueryWrapper.eq(ProductCategory::getPid, productCategory.getPid())
                .eq(ProductCategory::getName, productCategory.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("分类名称已存在");
        }
        save(productCategory);
    }

    @Override
    public void update(ProductCategory productCategory) {
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(ProductCategory::getId, productCategory.getId())
                .eq(ProductCategory::getPid, productCategory.getPid())
                .eq(ProductCategory::getName, productCategory.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("分类名称已存在");
        }
        ProductCategory category = getById(productCategory.getId());
        if (category != null) {
            // 父节点和ID相等，死循环问题
            if (Objects.equals(productCategory.getPid(), productCategory.getId())) {
                productCategory.setPid(category.getPid());
            }
            updateById(productCategory);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        List<ProductCategory> all = list();
        if (all != null && !all.isEmpty()) {
            List<TreeNode<ProductCategory>> treeNodes = all.stream().map(ProductCategory1 -> {
                TreeNode<ProductCategory> productCategoryTreeNode = new TreeNode<>();
                productCategoryTreeNode.setId(ProductCategory1.getId());
                productCategoryTreeNode.setName(ProductCategory1.getName());
                productCategoryTreeNode.setPid(ProductCategory1.getPid());
                productCategoryTreeNode.setTarget(ProductCategory1);

                return productCategoryTreeNode;
            }).collect(Collectors.toList());

            List<Long> deleteIds = new java.util.ArrayList<>(Collections.singletonList(id));
            List<TreeNode<ProductCategory>> treeNodes3 = TreeNodeUtil.allChildrenNodes(treeNodes, id);
            if (treeNodes3 != null && !treeNodes3.isEmpty()) {
                deleteIds.addAll(treeNodes3.stream().map(TreeNode::getId).collect(Collectors.toList()));
            }

            removeBatchByIds(deleteIds);
        }
    }

    @Override
    public List<ProductCategory> list(ProductCategoryReq ProductCategoryReq) {
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(ProductCategoryReq.getName())) {
            lambdaQueryWrapper.like(ProductCategory::getName, ProductCategoryReq.getName());
        }
        if (ProductCategoryReq.getStatus() != null && ProductCategoryReq.getStatus() > 0) {
            lambdaQueryWrapper.eq(ProductCategory::getStatus, ProductCategoryReq.getStatus());
        }
        if (ProductCategoryReq.getPid() != null && ProductCategoryReq.getPid() > 0) {
            lambdaQueryWrapper.eq(ProductCategory::getPid, ProductCategoryReq.getPid());
        }

        return list(lambdaQueryWrapper);
    }

    @Override
    public List<TreeNode<ProductCategory>> tree(ProductCategoryReq req) {
        if (req.getPid() == null) {
            req.setPid(0L);
        }
        List<ProductCategory> list = list(req);
        if (list != null && !list.isEmpty()) {
            List<TreeNode<ProductCategory>> treeNodes = list.stream().map(ProductCategory -> {
                TreeNode<ProductCategory> treeNode = new TreeNode<>();
                treeNode.setId(ProductCategory.getId());
                treeNode.setPid(ProductCategory.getPid());
                treeNode.setName(ProductCategory.getName());
                treeNode.setTarget(ProductCategory);

                return treeNode;
            }).collect(Collectors.toList());

            return TreeNodeUtil.buildTree(treeNodes, req.getPid());
        }
        return null;
    }
}
